package com.ihr360.payroll.service.payroll.mock.payrollDemo;

import com.ihr360.commons.exception.Ihr360RuntimeException;
import com.ihr360.commons.model.type.TaxRole;
import com.ihr360.expr.ExprArray;
import com.ihr360.payroll.config.SalaryTaxTableEntity;
import com.ihr360.payroll.dao.SalaryTaxTableRepository;
import com.ihr360.payroll.dao.SalaryTaxTypeRepository;
import com.ihr360.payroll.dao.TaxDivisionRepository;
import com.ihr360.payroll.model.dto.salary.basecode.SalaryTaxType;
import com.ihr360.payroll.model.dto.salary.basecode.TaxDivision;
import com.ihr360.payroll.model.dto.salary.meta.SalaryField;
import com.ihr360.payroll.model.dto.salary.tax.SalaryTaxTable;
import com.ihr360.payroll.model.dto.salary.tax.TaxTypeCode;
import com.ihr360.payroll.service.payroll.convertor.SalaryTaxTableConvertor;
import com.ihr360.payroll.service.payroll.mock.core.MockPersonalSalaryCalcCapable;
import com.ihr360.payroll.service.payroll.mock.core.MockPersonalSalaryCalcContextHolder;
import com.ihr360.payroll.service.payroll.mock.core.PersonalSalaryCalcCapable;
import com.ihr360.payroll.service.salarytasklet.model.SimplePayrollConfig;
import com.ihr360.payroll.service.salarytasklet.step.processor.TaxTableProcessor;
import com.ihr360.validation.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.Pair;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class PayrollConfigDemoSalaryCalcCapable extends MockPersonalSalaryCalcCapable implements PersonalSalaryCalcCapable {

    @Autowired
    private SalaryTaxTableRepository salaryTaxTableRepository;

    @Autowired
    private TaxDivisionRepository taxDivisionRepository;

    @Autowired
    private SalaryTaxTypeRepository salaryTaxTypeRepository;

    @Autowired
    private SalaryTaxTableConvertor salaryTaxTableConvertor;

    @Override
    public String staffId() {
        return MockPersonalSalaryCalcContextHolder.getStaffId();
    }

    @Override
    public String staffName() {
        return MockPersonalSalaryCalcContextHolder.getStaffName();
    }

    @Override
    public Pair<String, Map<String, ExprArray>> buildTaxTable() throws Exception {
        TaxTableProcessor processor = new TaxTableProcessor(salaryPeriodMonth());
        TaxDivision taxDivision = taxDivisionRepository.findByCode("ZHCN");
        SalaryTaxType salaryTaxType = salaryTaxTypeRepository.findByCode(TaxTypeCode.SALARY_TAX);

        SalaryTaxTable taxTable =
                salaryTaxTableRepository.findByTaxRoleAndDivisionAndTypeAndEnabled(TaxRole.NATIVE,
                        taxDivision,
                        salaryTaxType, true);

        if (taxTable == null) {
            throw new Ihr360RuntimeException("未找到税表数据！");
        }
        SalaryTaxTableEntity salaryTaxTableEntity = salaryTaxTableConvertor.convertDtoToEntity(taxTable);
        return processor.process(salaryTaxTableEntity);
    }

    @Override
    public SimplePayrollConfig buildPayrollConfig() {
        List<SalaryField> salaryFields = MockPersonalSalaryCalcContextHolder.getSalaryField();
        ValidationUtils.rejectIfEmpty(salaryFields, null, "请先设置薪资字段数据！");
        return new SimplePayrollConfig(null,null,salaryFields);
    }

}